package in.rln.touchplay;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class TouchPlay extends Activity {
	private static File EXT_DIR = Environment.getExternalStorageDirectory();
	private static String sEXT_DIR = EXT_DIR.getAbsolutePath()
			+ "/appdata/touchplay";
	private static final int SWIPE_MIN_DISTANCE = 50;
	private static final int SWIPE_THRESHOLD_VELOCITY = 50;
	public MediaPlayer mp = null;
	View.OnTouchListener gestureListener;
	private GestureDetector gestureDetector;
	ImageClickListener imageclickListener;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		gestureDetector = new GestureDetector(new SwipeGestureListener());
		imageclickListener = new ImageClickListener();
		drawImage(sEXT_DIR + "/elephant.jpg");
	}

	public void drawImage(String imgPath) {
		// Stop the sound if its still playing!!
		if (mp != null) {
			if (mp.isPlaying() || mp.isLooping()) {
				mp.stop();
			}
			mp = null;
		}

		ImageView view = (ImageView) findViewById(R.id.image);
		Drawable d = Drawable.createFromPath(imgPath);
		view.setImageDrawable(d);
		view.setOnClickListener(imageclickListener);
		view.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (gestureDetector.onTouchEvent(event)) {
					return true;
				} else
					return false;
			}
		});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		System.out.println("Main onTouch evt called");
		if (gestureDetector.onTouchEvent(event))
			return true;
		else
			return false;
	}

	class ImageClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			System.out.println("Clicked on image");
			mp = new MediaPlayer();
			try {
				mp.setDataSource(sEXT_DIR + "/bear.mp3");
				mp.prepare();
			} catch (IllegalArgumentException e) {
				System.out.println("Unable to find mp3");
				e.printStackTrace();
			} catch (IllegalStateException e) {
				System.out.println("In illegal state??");
				e.printStackTrace();
			} catch (IOException e) {
				System.out.println("IOException here!!");
				e.printStackTrace();
			}
			mp.start();
		}
	}

	class SwipeGestureListener extends SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			System.out.println("Fling called on image::" + e2.getX() + "::"
					+ e1.getX() + "::" + velocityX);
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				System.out.println("Displaying next image");
				drawImage(sEXT_DIR + "/giraffe.jpg");
				return true;
			} else {
				return false;
			}
		}
	}
}